package com.ksu.manager.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ksu.manager.model.BuyLoseManager;
import com.ksu.manager.model.TypeItem;
import com.ksu.manager.model.pages.Pagination;
import com.ksu.manager.service.IBuyLoseManagerService;
import com.ksu.manager.service.ITypeItemService;

@Controller
@RequestMapping("/buyLoseManager")
public class BuyLoseManagerController {
	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(BuyLoseManagerController.class);
	
	private IBuyLoseManagerService buyLoseManagerService;
	
	private ITypeItemService typeItemService;


	public void setTypeItemService(ITypeItemService typeItemService) {
		this.typeItemService = typeItemService;
	}
	
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/list")
	public ModelAndView queryList(HttpServletRequest request, HttpServletResponse response, BuyLoseManager buyLoseManager, Pagination page) {
		ModelAndView mv = new ModelAndView("/buyLoseManager/list");
		try {
			Map<String,Object> map = buyLoseManagerService.queryList(buyLoseManager,page);
			List<BuyLoseManager> buyLoseManagerList = null;
			if(map!=null && map.size()>0){
			   buyLoseManagerList = (List<BuyLoseManager>)map.get("list");
			   mv.addObject("page",map.get("page"));
			}
			List<TypeItem> typeItemList = typeItemService.queryList(new TypeItem());
			mv.addObject("buyLoseManagerList", buyLoseManagerList);
			mv.addObject("typeItemList", typeItemList);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return mv;
	}
	
	
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpServletRequest request, HttpServletResponse response,BuyLoseManager buyLoseManager){
		if(buyLoseManager!=null){
			buyLoseManagerService.add(buyLoseManager);
		}
		Pagination page = new Pagination();
		page.init();
		return queryList(request,response,buyLoseManager,page);
	}
	
	@RequestMapping(value = "/del")
	public ModelAndView del(HttpServletRequest request, HttpServletResponse response,BuyLoseManager buyLoseManager){
		if(buyLoseManager!=null){
			buyLoseManagerService.del(buyLoseManager);
		}
		Pagination page = new Pagination();
		page.init();
		return queryList(request,response,buyLoseManager,page);
	}


	public void setBuyLoseManagerService(IBuyLoseManagerService buyLoseManagerService) {
		this.buyLoseManagerService = buyLoseManagerService;
	}
	

}
